package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import com.leyou.item.vo.SpecGroupDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *
 *
 * @Author: Rose
 * @Date: 2021/2/14 16:24
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;


    /**
     *  根据分类id 查询规格组
     *
     *
     * @param cid
     * @return
     */
    @GetMapping(value = "/groups/{cid}")
    public ResponseEntity<List<SpecGroup>>  queryGroupByCid(@PathVariable("cid") Long cid ) {

        List<SpecGroup> list =  specificationService.queryGroupByCid(cid);

        return ResponseEntity.ok(list);
    }


    /**
     *
     *  根据分类id 新增规格组
     *
     * @param
     * @param
     * @return
     */
    @PostMapping(value = "/group")
    public ResponseEntity<Void>  addGroupByCid( @RequestParam(value = "cid",required = true,defaultValue = "0") Long cid,
                                               @RequestParam(value = "name",required = true,defaultValue = "")String name  )
    //public ResponseEntity<Void>  addGroupByCid(@RequestBody SpecGroup specGroup )
    {

        //specificationService.addGroupByCid(specGroup.getCid(),specGroup.getName());
        specificationService.addGroupByCid(cid,name);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     *  更新规格组
     *
     * @param cid
     * @param name
     * @param id
     * @return
     */
    @PutMapping(value = "/group")
    public ResponseEntity<Void>  updateGroupByCid( @RequestParam(value = "cid",required = true,defaultValue = "0") Long cid,
                                                   @RequestParam(value = "name",required = true,defaultValue = "")String name ,
                                                   @RequestParam(value = "id",required = true, defaultValue = "0")Long id ) {

        specificationService.updateGroupByCid(cid,name,id);

        return ResponseEntity.status(HttpStatus.RESET_CONTENT).build();
    }


    /**
     *
     *  删除规格组
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/groups/{id}")
    public ResponseEntity<Void>  deleteGroupById(@PathVariable("id") Long id ) {

        specificationService.deleteGroupById(id);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *
     *  保存规格参数
     *
     *
     * @param cid
     * @param groupId
     * @param segments
     * @param numeric
     * @param searching
     * @param generic
     * @param name
     * @param unit
     * @return
     */

    @PostMapping("params")
    public ResponseEntity<Void> saveParam(
            @RequestParam("cid") Long cid ,
            @RequestParam("groupId") Long groupId ,
            @RequestParam("segments") String segments ,
            @RequestParam("numeric") Boolean numeric ,
            @RequestParam("searching") Boolean searching ,
            @RequestParam("generic") Boolean generic ,
            @RequestParam("name") String name ,
            @RequestParam(value = "unit",required = false) String unit
            ){
        specificationService.saveParam(cid,groupId,segments,numeric,searching,generic,name,unit);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 根据组id 查询 商品规格参数
     *
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParamList(@RequestParam(value = "gid", required = false) Long gid,
                                                          @RequestParam(value = "searching", required = false) Boolean searching,
                                                          @RequestParam(value = "cid", required = false) Long cid) {
        List<SpecParam> specParamList = specificationService.queryParamList(gid, cid, searching);
        return ResponseEntity.ok(specParamList);
    }


    /**
     *
     *  更新 商品规格参数
     *
     * @param cid
     * @param groupId
     * @param segments
     * @param numeric
     * @param searching
     * @param generic
     * @param name
     * @param unit
     * @return
     */
    @PutMapping("params")
    public ResponseEntity<Void> updateParam(

            @RequestParam("id") Long id ,
            @RequestParam("cid") Long cid ,
            @RequestParam("groupId") Long groupId ,
            @RequestParam("segments") String segments ,
            @RequestParam("numeric") Boolean numeric ,
            @RequestParam("searching") Boolean searching ,
            @RequestParam("generic") Boolean generic ,
            @RequestParam("name") String name ,
            @RequestParam(value = "unit",required = false) String unit
    ){
        specificationService.updateParam(id,cid,groupId,segments,numeric,searching,generic,name,unit);
        return ResponseEntity.status(HttpStatus.RESET_CONTENT).build();
    }


    /**
     *  根据规格参数组 主键id 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("params/{id}")
    public ResponseEntity<Void> updateParam(@PathVariable("id") Long id) {
        specificationService.deleteSpecParamById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("group")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCid(@RequestParam("cid") Long cid) {
        List<SpecGroupDTO> specGroupDTOList = this.specificationService.querySpecGroupByCid(cid);
        return ResponseEntity.ok(specGroupDTOList);
    }
}
